
#include "StdAfx.h"
#include "RoomExit_Default.h"
#include "LobbyService.h"
#include "Room.h"
#include "RoomManager.h"
#include "LobbyManager.h"
#include "User.h"
#include "board_define.h"

int RoomExit_Default::exit( User* user, int type, bool force, bool to_room ){
	int result = RoomExit_Decorator::exit( user, force );
	if( result )
		return result;
	
	Room* room = user->getRoom();
	if(! room )
		return 0;

	if(! force ){
		result = room->exitCheck( user, type );
		if( result )
			return result;
	}	
	
	if( ( LobbyService::Instance()->getRoomUserMax() > 1 ) &&
		user->isTypeNormal() )
	{
		room->sendUserExit( user );	
	}

	room->exitUser( user );

	int remain_count = room->removeUser( user->getIdent() );
	user->setRoom( 0 );

	if( ( remain_count == DEF_ERROR_INTERNAL ) &&
		( room->getUserCount() == 0 ) )
	{
		remain_count = 0;
	}

	LobbyManager* channelMgr = LobbyService::Instance()->getLobbyManager();

	if(! to_room )
		channelMgr->sendRoomListInfoUpdate( room );	

	if(! remain_count ){	
		RoomManager* roomMgr = LobbyService::Instance()->getRoomManager();
		channelMgr->sendRemoveRoom( room );
		roomMgr->add_delete_room( room );
	}

	return 0;
}
